package com.sdtele.tta.operation;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.Task;
import com.sdtele.tta.models.TaskOperation;
import com.sdtele.tta.models.User;

/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationCreateTask extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		User curUser = (User) request.getSession().getAttribute("user");
		if (curUser != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			Task newtask = new Task();
			long ownerId = curUser.getId();
			String title = jsonRequest.getJsonObject().get("title").getAsString();

			Timestamp createTime = new Timestamp(System.currentTimeMillis());
			Timestamp updateTime = new Timestamp(System.currentTimeMillis());

			if ((jsonRequest.getJsonObject().get("deadLine")!=null) && 
					(!jsonRequest.getJsonObject().get("deadLine").getAsString().isEmpty()))

			{
				java.util.Date ud = new java.util.Date();
				String deadLineStr = jsonRequest.getJsonObject().get("deadLine").getAsString();
				
				System.out.println("请求输入时间："+deadLineStr);
				try {
					
					ud = sdf.parse(deadLineStr);
					System.out.println("格式化后时间："+ud);
				} catch (ParseException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}

				Date deadLine = new java.sql.Date(ud.getTime());
				System.out.println("比较时间："+deadLine);
				java.util.Date sysDate = new java.util.Date();
				String sysDateStr = sdf.format(sysDate);

				try {
					sysDate = sdf.parse(sysDateStr);
				} catch (ParseException e1) {
					// TODO 自动生成的 catch 块
					e1.printStackTrace();
				}

				Calendar c = Calendar.getInstance();

				c.setTime(sysDate);
				int day = c.get(Calendar.DATE);
				c.set(Calendar.DATE, day - 1);

				sysDate = c.getTime();

				if (ud.compareTo(sysDate) > 0)

				{
					System.out.println("设置数据库时间："+deadLine);
					newtask.setDeadLine(deadLine);
				} else {
					jres.setStatus(1);
					jres.setMessage("完成日期不能早于当前日期");
					return jres;
				}

			}
			if (jsonRequest.getJsonObject().get("fatherTaskId") != null) {
				long fatherTaskId = jsonRequest.getJsonObject().get("fatherTaskId").getAsLong();
				newtask.setFatherTaskId(fatherTaskId);
			}
			newtask.setOwnerId(ownerId);
			newtask.setTitle(title);
			newtask.setCreateTime(createTime);
			newtask.setUpdateTime(updateTime);

			Dac.getInstance().addTask(newtask);

			if (newtask.getId() > 0) {
				TaskOperation taskOp = new TaskOperation();

				taskOp.setTaskId(newtask.getId());
				taskOp.setUserId(ownerId);
				taskOp.setOperation(0);
				Dac.getInstance().addOperation(taskOp);

				if (jsonRequest.getJsonObject().get("implementId") != null) {
					long implementId = jsonRequest.getJsonObject().get("implementId").getAsLong();
					if (ownerId != implementId) {
						TaskOperation taskOp1 = new TaskOperation();
						taskOp1.setTaskId(newtask.getId());
						taskOp1.setUserId(implementId);
						taskOp1.setOperation(1);

						Dac.getInstance().addOperation(taskOp1);
					}
				}
				jres.setStatus(0);
				jres.setMessage("创建任务成功");

				Task task = Dac.getInstance().getTaskbyTaskid(newtask.getId());
				map.put("task", task.ToTaskOut());

				User owner = Dac.getInstance().getUser(task.getOwnerId());
				map.put("owner", owner);
				map.put("reader", 0);

				jres.setEntity(map);
			} else {
				jres.setStatus(1);
				jres.setMessage("创建任务失败");
			}
		}
		return jres;
	}

}
